package efu.nf.like.service.impl;

import efu.nf.like.mapper.PostMapper;
import efu.nf.like.model.dto.PostDTO;
import efu.nf.like.model.entity.Post;
import efu.nf.like.service.LikeService;
import efu.nf.like.service.PostService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lwj
 * @create 2025-10-12-17:01
 */
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional
public class PostServiceImpl implements PostService {
    private final PostMapper postMapper;
    private final LikeService likeService;

    @Override
    public List<PostDTO> listPosts(Integer userId) {
        // 前端接收的帖子列表
        List<PostDTO> postDTOS = new ArrayList<>();
        List<Post> posts = postMapper.listPosts();
        // 遍历帖子列表
        posts.forEach(post -> {
            PostDTO postDTO = new PostDTO();
            postDTO.setPost(post);
            // 设置当前用户是否点赞了该帖子
            postDTO.setUserLikeStatus(likeService.isLike(post.getPostId(), userId));
            // 添加到帖子列表中
            postDTOS.add(postDTO);
        });
        return postDTOS;
    }

    @Override
    public void update(Post post) {
        postMapper.update(post);
    }

    @Override
    public void insert(Post post) {
        postMapper.insert(post);
    }

    @Override
    public void delete(Integer postId) {
        postMapper.delete(postId);
    }
}
